<?php
/**
 *
 * (model)
 */
namespace App\Models\SalesReport;

use App\Models\Basics\SysCountry;
use App\Models\Basics\SysArea;
use App\Models\Basics\SysPlat;
use App\Models\Product\ProductClassify;
use Illuminate\Database\Eloquent\Model;
use App\Util\User;
class MonthlyCv extends Model
{
    /**
     * 月销量统计报表-品类
     * @var string
     */
    protected $table = 'sr_monthly_cv';

    protected $guarded = [];

    public $timestamps = false;

    const V_NUM = 16;

    public function classify(){
        return $this->belongsTo(ProductClassify::class,'classify_id');
    }

    public function country(){
        return $this->belongsTo(SysCountry::class,'country_id');
    }

    public function area(){
        return $this->belongsTo(SysArea::class,'area_id');
    }

    public function plat(){
        return $this->belongsTo(SysPlat::class,'plat_id','plat_id');
    }

    public static function headersField(){
        $fields = [];
        $fields['plat'] = ['title' => '平台','sumField' => false];
        $fields['area'] = ['title' => '站点','sumField' => false];
        $fields['country'] = ['title' => '国家','sumField' => false];
        $fields['seller'] = ['title' => '销售','sumField' => false];
        $fields['season'] = ['title' => '季节性','sumField' => false];
        $fields['classify'] = ['title' => '品类','sumField' => false];
        for($i = self::V_NUM - 1; $i >= 0; $i--){
            $fields['month'.$i] = [
                'title' => date("Y.m",strtotime(date('Y-m-01') . "-$i month")),
                'sumField' => true
            ];
        }
        $fields['month_target'] = ['title' => '当月目标','sumField' => true];
        $fields['comp_rate'] = ['title' => '完成率','sumField' => false];
        return $fields;
    }

    public static function getDownloadData($re){
        foreach ($re as $row){
            $temp = [];
            $temp['plat'] = $row->plat->title ?? '';
            $temp['area'] = $row->area->area ?? '';
            $temp['country'] = $row->country->country ?? '';
            $temp['seller'] = User::getUsernameById($row->seller_id);
            $temp['season'] = ProductClassify::$_season[$row->classify->season ?? 3];
            $temp['classify'] = $row->classify->title ?? '';
            $yttLast = MonthlyCTarget::query()
                ->where('monthly',MonthlyCTarget::START_DATE_LAST)
                ->where('country_id',$row->country_id)
                ->where('classify_id',$row->classify_id)
                ->first();
            $ytt = MonthlyCTarget::query()
                ->where('monthly',MonthlyCTarget::START_DATE)
                ->where('country_id',$row->country_id)
                ->where('classify_id',$row->classify_id)
                ->first();
            $startTimeLast = strtotime(MonthlyCTarget::START_DATE_LAST);
            $startTime = strtotime(MonthlyCTarget::START_DATE);
            for($i = self::V_NUM - 1; $i >= 0; $i--){
                $thisTime = strtotime(date('Y-m-01') . "-$i month");
                if($thisTime < $startTime){
                    $num = getMonthDiffByDateInt($startTimeLast,$thisTime);
                    $temp['month'.$i] = $row->{'month'.$i};
                    $temp['month_target'.$i] = $yttLast->{'estimate_volume'.$num} ?? 0;
                    $temp['comp_rate'.$i] = $temp['month'.$i] > 0 && $temp['month_target'.$i] > 0 ? numberFormat($temp['month'.$i] / $temp['month_target'.$i],4) * 100 .'%' : 0;
                }else{
                    if($i != 0){
                        $num = getMonthDiffByDateInt($startTime,$thisTime);
                        $temp['month'.$i] = $row->{'month'.$i};
                        $temp['month_target'.$i] = $ytt->{'estimate_volume'.$num} ?? 0;
                        $temp['comp_rate'.$i] = $temp['month'.$i] > 0 && $temp['month_target'.$i] > 0 ? numberFormat($temp['month'.$i] / $temp['month_target'.$i],4) * 100 .'%' : 0;
                    }else{
                        $temp['month'.$i] = $row->{'month'.$i};
                    }
                }
            }
            $temp['month_target'] = $row->month_target;
            $temp['comp_rate'] = $row->month0 > 0 && $row->month_target > 0 ? numberFormat($row->month0 / $row->month_target,4) * 100 .'%' : 0;
            $endTime = strtotime(MonthlyCTarget::END_DATE);
            for($i = 1; $i <= MonthlyCTarget::E_NUM -1; $i++){
                $_thisTime = strtotime(date('Y-m-01') . "+$i month");
                if($_thisTime > $endTime)break;
                $num = getMonthDiffByDateInt($startTime,$_thisTime);
                $temp['month_target_'.$i] = $ytt->{'estimate_volume'.$num} ?? 0;
            }
            yield $temp;
        }
    }
}
